package org.lw.bootstrap.dto;

import org.lw.bootstrap.dto.exception.BizException;

/**
 * 
 *
 */
public class ResultFactory {

	/**
	 * 
	 * @return
	 */
	public static Result succeed() {
		return succeed(null);
	}
	
	/**
	 * succeed
	 * @param data
	 * @return
	 */
	public static Result succeed(Object data) {
		return succeed(data, null);
	}
	
	public static Result succeed(Object data, Object attachment) {
		return new Result(ErrorCode.SUCCEED.name(), null, data, attachment);
	}
	
	/**
	 * 
	 * @param msg
	 * @return
	 */
	public static Result failed(String msg) {
		return failed(ErrorCode.SYSTEM_ERROR.name(), msg);
	}
	
	/**
	 * 
	 * @param e
	 * @return
	 */
	public static Result failed(BizException e) {
		return newResult(e.getErrorCode(), e.getMsg(), null);
	}
	
	/**
	 * failed
	 * @param ec
	 * @param msg
	 * @return
	 */
	public static Result failed(String errorCode, String msg) {
		return newResult(errorCode, msg, null);
	}
	
	/**
	 * failed
	 * @param errorCode
	 * @return
	 */
	public static Result failed(ErrorCode errorCode) {
		return newResult(errorCode.name(), errorCode.getMsg(), null);
	}
	
	/**
	 * failed
	 * @param errorCode
	 * @param msg
	 * @return
	 */
	public static Result failed(ErrorCode errorCode, Object... args) {
		return newResult(errorCode.name(), String.format(errorCode.getMsg(), args), null);
	}
	
	/**
	 * 
	 * @param errorCode
	 * @param msg
	 * @param data
	 * @return
	 */
	public static Result newResult(String errorCode, String msg, Object data) {
		return new Result(errorCode, msg, data);
	}
}
